﻿namespace TVersityDotNet.MediaLib
{
    using System;
    using System.Globalization;
    using System.Runtime.Serialization;
    using System.Xml.Linq;

    [DataContractAttribute]
    public class MediaLibMusicItem : MediaLibMediaItem
    {
        [DataMember]
        public string Artist { get; set; }
        [DataMember]
        public int OriginalTrackNumber { get; set; }
        [DataMember]
        public DateTime Date { get; set; }
        [DataMember]
        public int NBAudioChannels { get; set; }
        [DataMember]
        public int Bitrate { get; set; }
        [DataMember]
        public int SampleFrequency { get; set; }
        [DataMember]
        public TimeSpan Duration { get; set; }

        public MediaLibMusicItem(XElement xmlElement)
        {
            this.SearchClass = MediaLibClassType.MusicItem;
            base.RawXml = xmlElement;
            this.ProcessXmlElement();
        }

        public new void ProcessXmlElement()
        {
            base.ProcessXmlElement();
            this.Artist = this.RawXml.Element(XName.Get("artist", MediaLibResponseItem.Namespace_Upnp)).Value;
            if (this.RawXml.Element(XName.Get("originalTrackNumber", MediaLibResponseItem.Namespace_Upnp)) != null)
                this.OriginalTrackNumber = int.Parse(this.RawXml.Element(XName.Get("originalTrackNumber", MediaLibResponseItem.Namespace_Upnp)).Value, CultureInfo.InvariantCulture);
            if (this.RawXml.Element(XName.Get("date", MediaLibResponseItem.Namespace_DC)) != null)
                this.Date = DateTime.Parse(this.RawXml.Element(XName.Get("date", MediaLibResponseItem.Namespace_DC)).Value, CultureInfo.InvariantCulture);
            else 
                this.Date = new DateTime();
            this.NBAudioChannels = int.Parse(this.ResElement.Attribute("nrAudioChannels").Value, CultureInfo.InvariantCulture);
            this.SampleFrequency = int.Parse(this.ResElement.Attribute("sampleFrequency").Value, CultureInfo.InvariantCulture);
            if (this.ResElement.Attribute("bitrate") != null)
                this.Bitrate = int.Parse(this.ResElement.Attribute("bitrate").Value, CultureInfo.InvariantCulture);
            else
                this.Bitrate = 0;
            this.Duration = TimeSpan.Parse(this.ResElement.Attribute("duration").Value, CultureInfo.InvariantCulture);                            
        }
    }
}
